package com.example.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.common.CustomException;
import com.example.demo.entity.DefenseGroup;
import com.example.demo.mapper.DefenseGroupMapper;
import com.example.demo.mapper.DefenseGroupPersonMapper;
import com.example.demo.mapper.DefenseGroupStudentMapper;
import com.example.demo.mapper.StudentSubjectTeacherMapper;
import com.example.demo.model.DefenseGroupDto;
import com.example.demo.model.DefenseGroupPersonDto;
import com.example.demo.model.DefenseGroupStudentDto;
import com.example.demo.model.StudentSubjectTeacherDto;
import com.example.demo.service.IDefenseGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 答辩组基本信息表 服务实现类
 * </p>
 *
 * @author liwei
 * @since 2024-11-15
 */
@Service
@Slf4j
public class DefenseGroupServiceImpl extends ServiceImpl<DefenseGroupMapper, DefenseGroup> implements IDefenseGroupService {

    @Autowired
    private DefenseGroupStudentMapper defenseGroupStudentMapper;

    @Autowired
    private DefenseGroupMapper defenseGroupMapper;

    @Autowired
    private DefenseGroupPersonMapper defenseGroupPersonMapper;


    @Autowired
    private StudentSubjectTeacherMapper subjectTeacherMapper;

    /* *
     * @Title: getDefenseGroupById
     * @Author: admin
     * @Date: 2024-11-22 10:57:35
     * @Params: [defenseGroupId] 答辩组编号
     * @Return: DefenseGroupDto
     * @Description: 根据答辩组ID查询答辩组的详细信息
     */
    @Override
    public DefenseGroupDto getDefenseGroupById(Long defenseGroupId) {
        DefenseGroup defenseGroup = defenseGroupMapper.selectById(defenseGroupId);
        DefenseGroupDto defenseGroupDto = new DefenseGroupDto();
        BeanUtil.copyProperties(defenseGroup, defenseGroupDto);
        if (defenseGroup == null) {
            return defenseGroupDto;
        }

        List<DefenseGroupStudentDto> defenseGroupStudentDtos = defenseGroupStudentMapper.getDefenseGroupStudentByGroupId(defenseGroupId);
        if (defenseGroupStudentDtos != null && !defenseGroupStudentDtos.isEmpty()) {
            defenseGroupDto.setDefenseGroupStudentList(defenseGroupStudentDtos);
        }
        List<DefenseGroupPersonDto> defenseGroupPersonDtos = defenseGroupPersonMapper.getDefenseGroupPersonByGroupId(defenseGroupId);
        if (defenseGroupPersonDtos != null && !defenseGroupPersonDtos.isEmpty()) {
            defenseGroupDto.setDefenseGroupPersonList(defenseGroupPersonDtos);
        }
        return defenseGroupDto;
    }

    /* *
     * @Title: addDefenseGroup
     * @Author: admin
     * @Date: 2024-11-28 10:25:55
     * @Params: [dto]
     * @Return: Boolean
     * @Description: 添加答辩组--带事务
     */
    @Override
    @DSTransactional(rollbackFor = Exception.class)
    public Boolean addDefenseGroup(DefenseGroupDto dto) {
        //判断是否有答辩组重名
        LambdaQueryWrapper<DefenseGroup> defenseGroupLambdaQueryWrapper = new LambdaQueryWrapper<>();
        defenseGroupLambdaQueryWrapper.eq(DefenseGroup::getDefenseGroupName, dto.getDefenseGroupName());
        List<DefenseGroup> defenseGroupList = defenseGroupMapper.selectList(defenseGroupLambdaQueryWrapper);
        if (defenseGroupList != null && !defenseGroupList.isEmpty()) {
            throw new CustomException("答辩组名称已存在！");
        }
        DefenseGroup defenseGroupEntity = BeanUtil.toBean(dto, DefenseGroup.class);
        int defenseGroupInsert = defenseGroupMapper.insert(defenseGroupEntity);
        log.info("插入result:{},defenseGroupId:{}", defenseGroupInsert, defenseGroupEntity.getDefenseGroupId());
        defenseGroupEntity.setOriginalDefenseGroupId(defenseGroupEntity.getDefenseGroupId());
        int defenseGroupUpdate = defenseGroupMapper.updateById(defenseGroupEntity);
        log.info("更新result:{}", defenseGroupUpdate);
        if (dto.getDefenseGroupPersonList() != null && !dto.getDefenseGroupPersonList().isEmpty()) {
            //添加答辩组组长、答辩组老师、答辩组录入员
        }
        if (dto.getDefenseGroupStudentList() != null && !dto.getDefenseGroupStudentList().isEmpty()) {
            //添加答辩组学生
            for (int i = 0; i < dto.getDefenseGroupStudentList().size(); i++) {
                QueryWrapper subjectTeacherMapperQueryWrapper = new QueryWrapper();
                subjectTeacherMapperQueryWrapper.eq("sy.student_id", dto.getDefenseGroupStudentList().get(i).getStudentId());
                subjectTeacherMapperQueryWrapper.eq("sy.subject_id", dto.getDefenseGroupStudentList().get(i).getSubjectId());
                subjectTeacherMapperQueryWrapper.eq("sy.academic_year_id", dto.getDefenseGroupStudentList().get(i).getAcademicYearId());
                List<StudentSubjectTeacherDto> studentSubjectTeacherDtos = subjectTeacherMapper.getStudentSubjectTeachers(subjectTeacherMapperQueryWrapper);
                if (studentSubjectTeacherDtos.isEmpty() || studentSubjectTeacherDtos.size() > 1) {
                    throw new CustomException("学生信息不合法！");
                }
            }
        }

        return true;
    }

    /* *
     * @Title: deleteDefenseGroup
     * @Author: admin
     * @Date: 2024-11-28 16:48:46
     * @Params: [dto]
     * @Return: Boolean
     * @Description: 删除答辩组--物理删除
     */
    @Override
    public int deleteDefenseGroup(Long defenseGroupId) {
        DefenseGroup defenseGroup = defenseGroupMapper.selectById(defenseGroupId);
        if (defenseGroup == null) {
            throw new CustomException("未找到答辩组");
        }

        return defenseGroupMapper.deleteById(defenseGroupId);
    }


}

